<template>
  <div class="f2_box">
    <div class="nav">
      <span :class="{ active: cur == 1 }" @click="cur = 1">首页</span>
      <span :class="{ active: cur == 2 }" @click="cur = 2">各方反应</span>
      <span :class="{ active: cur == 3 }" @click="cur = 3">全球连线</span>
      <span :class="{ active: cur == 4 }" @click="cur = 4">高清大图</span>
      <span :class="{ active: cur == 5 }" @click="cur = 5">更多</span>
    </div>
    <div class="list" v-for="item in data" :key="item.nid">
      <router-link :to="`/article?nid=${item.nid}`">
        <div class="news">
          <div class="text">
            <span>{{ item.nname }}</span>
            <span>{{ item.disc }}</span>
          </div>
          <img v-if="item.ncover" :src="item.ncover" />
          <img
            v-else
            src="https://p3-search.byteimg.com/img/tos-cn-i-qvj2lq49k0/ed9e61b7fe9b416d9517e5280f4b3110~tplv-26tn0yjwph-cspd-v1:276:184.webp"
          />
        </div>
      </router-link>
    </div>
    <div class="next_list">
      <span v-if="isFinish">已经到底了</span>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      cur: 1,
      data: "",
      page: 1,
      isLoading: false,
      isFinish: false,
    };
  },
  methods: {
    getData(cid, page, callback) {
      let url = `http://120.78.224.75:6677/articles?cid=${cid}&page=${page}`;
      if (cid > 4) {
        url = `http://120.78.224.75:6677/loadall?page=${page}`;
      }
      this.axios.get(url).then((res) => {
        let articleList = res.data;
        // 调用callback方法，传入articleList，返回文章列表
        callback(articleList);
      });
    },
  },
  mounted() {
    let _this = this;
    window.onscroll = function () {
      // 滚动条滚动时，距离顶部的距离
      let scrollTop =
        document.documentElement.scrollTop || document.body.scrollTop;
      // 可视区的高度
      let windowHeight =
        document.documentElement.clientHeight || document.body.clientHeight;
      // 滚动条的而总高度
      let scrollHeight =
        document.documentElement.scrollHeight || document.body.scrollHeight;
      if (scrollTop + windowHeight >= scrollHeight - 10) {
        // 将isLoading改为true，代表正在加载中，不接受再次加载
        _this.isLoading = true;
        setTimeout(() => {
          _this.page++;
          _this.getData(_this.cur, _this.page, (articleList) => {
            if (_this.page > articleList.pagecount) {
              _this.isFinish = true;
              _this.isLoading = false;
              return;
            }
            _this.data.push(...articleList.results);
            console.log(`正在加载第${_this.page}页`);
            // 加载结束，将isLoading改为false，可再次加载新数据
            _this.isLoading = false;
          });
        }, 500);
      }
    };
    console.log(this.$route.meta.keepAlive);
    this.getData(this.cur, this.page, (articleList) => {
      // console.log(articleList);
      this.data = articleList.results;
    });
  },
  watch: {
    cur(newValue, oldValue) {
      this.page = 1;
      this.isFinish = false;
      this.getData(newValue, this.page, (articleList) => {
        this.data = articleList.results;
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.f2_box {
  font-family: "SF Pro Display", "PingFang SC", "Hiragino Sans GB",
    "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", "Arial",
    "sans-serif";
  background-color: #f8f8f8;
  margin-bottom: 18px;
  > .nav {
    display: flex;
    color: #1a1a1a;
    font-size: 22px;
    justify-content: space-between;
    padding: 0 25px;
    border-bottom: 1px solid #bbb;
    cursor: pointer;
    > span {
      padding: 15px 15px;
      // transition: all 0.25s;
      &.active {
        color: #9d252a;
        border-bottom: 2px solid #9d252a;
      }
      &:not(.active):hover {
        opacity: 0.8;
      }
    }
  }
  .list {
    padding: 0 40px;
    .news {
      width: 596px;
      padding: 10px 0;
      border-bottom: 1px solid #bbb;
      display: flex;
      > .text {
        width: 401px;
        padding: 10px 30px 10px 5px;
        > span {
          color: #222;
          width: 100%;
          font-size: 16px;
          display: inline-block;
          &:first-child {
            color: black;
            font-size: 20px;
            font-weight: 600;
            margin-bottom: 10px;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
            cursor: pointer;
          }
          &:last-child {
            line-height: 20px;
            overflow: hidden;
            text-overflow: ellipsis;
            display: -webkit-box;
            -webkit-line-clamp: 3;
            -webkit-box-orient: vertical;
          }
        }
      }
    }
    img {
      width: 160px;
      height: 120px;
    }
  }
}
.next_list {
  width: 676;
  height: 10px;
  > span {
    display: block;
    text-align: center;
    font-size: 14px;
    color: #999;
    padding: 5px 0;
  }
}
</style>
